#!/usr/bin/python
#-*-coding:utf-8-*-

###############################################################
## Name     : auto_testbench
## Author   : GJM 
## Time     : 2024-12-24 11:37:12
## Description:
## 
## 
###############################################################

import sys
import os
import re
import argparse
import math
import difflib

def debug_print(*ln): #{{{
    if debug:
        print(ln)
pass #}}}

def write_list(list): #{{{
  for line in list:
    print(line)
pass #}}}

def input_args_proc(): #{{{
  global debug
  global ver
  global pwd_path
  global pwd_dict
  global demo_en

  parser = argparse.ArgumentParser(description="argparse info")
  parser.add_argument('-o', action='store_true', default=False, help='open this script')
  parser.add_argument('-d', action='store_true', default=False, help='debug mode')
  parser.add_argument('-f', default=False, help='file path')
  result = parser.parse_args()
  
  debug = result.d
  if result.o == True:
    os.system("gvim %s" % __file__)
    sys.exit(0)

  if result.f == False:
    top = "demo"
    path = "demo"
    demo_en = True
  else:
    file = os.path.abspath(result.f) #result.f
    top = os.path.splitext(os.path.basename(file))[0]
    path = os.path.dirname(os.path.abspath(file))
    demo_en = False

  pwd_dict = os.getcwd() + "/"
  pwd_path = os.getcwd() + "/" + result.f
  debug_print(top, path, file)
  return top, path, file
pass #}}}

def gen_dc_demo(top, path, file): #{{{
  global work_path
  run_list = []
  
  source_path = path #rtl所在目录
  root_path = sys.path[0] #脚本所在目录
  work_path = "{0}/{1}_dc_demo".format(os.getcwd(), top) #生成文件的目录
  
  root_run_path = "{0}/run.tcl".format(root_path)
  demo_run_path = "{0}/run.tcl".format(work_path)
  
  os.system("mkdir {0} -p".format(work_path))
  os.system("cp {0}/Makefile {1}/ -p".format(root_path, work_path))
  os.system("cp {0}/*.db {1}/ -p".format(root_path, work_path))

  with open(root_run_path, "r") as tcl:
    handle = tcl.readlines()
    for line in handle:
      line = re.sub("SOURCE_DIR", source_path, line)
      line = re.sub("WORKING_DIR", work_path, line)
      line = re.sub("TOP_MODULE_FILE", file, line)
      line = re.sub("TOP_MODULE", top, line)
      run_list.append(line)
  with open(demo_run_path, "w") as tcl:
    for line in run_list:
      tcl.write(line)
pass #}}}

def main(): #{{{
  (top, path, file) = input_args_proc()
  gen_dc_demo(top, path, file)
  print("||================================================================")
  print("|| Please: \n|| cd {0} \n|| make syn [gui=off/on] [db=off/130fast/130slow]".format(work_path))
  print("||================================================================")
pass #}}}

if __name__ == "__main__":
  print("%s is work" % __file__)
  main()
